//
//  MYShopCarCell.swift
//  MuYing
//
//  Created by tbb on 2019/7/30.
//  Copyright © 2019 TBB. All rights reserved.
//specification

import UIKit

@objc protocol MYGoodCarCellDelegate:NSObjectProtocol {
    
    func didSelectGoodActionModifyItem(_ item:String ,_ andModel:YQPCartModel);

}

class MYGoodCarCell: YQTableViewBaseCell,UITextFieldDelegate {
    
    @objc var cartModel = YQPCartModel()
    
    @objc weak var delegate: MYGoodCarCellDelegate?

    @objc @IBOutlet weak var lineView: UIView!

    @IBOutlet weak var countLb: UILabel!
    @IBOutlet weak var selectBtn: UIButton!
    @IBOutlet weak var goodsIcon: UIImageView!
    
    @IBOutlet weak var titleLb: UILabel!
    

    @IBOutlet weak var specificationLb: UILabel!
    
    @IBOutlet weak var priceLb: UILabel!
    
    @IBOutlet weak var reductionBtn: UIButton!
    
    @IBOutlet weak var addBtn: UIButton!
    
    @IBOutlet weak var countTextField: UITextField!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.lineView.isHidden = true;
        self.countTextField.keyboardType = .numberPad;
        self.countTextField.delegate = self;
        self.selectBtn.isUserInteractionEnabled = false
        self.selectBtn.setImage(UIImage(named: "shopCar_select_d_icon"), for: .normal)
        self.selectBtn.setImage(UIImage(named: "shopCar_select_s_icon"), for: .selected)
        
        self.goodsIcon.layer.cornerRadius = 6;
        self.goodsIcon.clipsToBounds = true;
        
        
    }

    @IBAction func reductionAction(_ sender: UIButton) {
        let text = self.countTextField.text as NSString?;
        guard var num = text?.integerValue else {
            return;
        }
        
        if num < 2 {
            return;
        }
        num = num - 1;
        let numStr = String(num);
        self.countTextField.text = numStr;
        
        if let delegate = self.delegate, delegate.responds(to: Selector(("didSelectGoodActionModifyItem::"))) {
            self.delegate?.didSelectGoodActionModifyItem(self.countTextField?.text ?? "1",self.cartModel);
        }
    }
    
    @IBAction func addAction(_ sender: UIButton) {
        let text = self.countTextField.text as NSString?;
        guard var num = text?.integerValue else {
            return;
        }
        num = num + 1;
        let numStr = String(num);
        self.countTextField.text = numStr;
        
        if let delegate = self.delegate, delegate.responds(to: Selector(("didSelectGoodActionModifyItem::"))) {
            self.delegate?.didSelectGoodActionModifyItem(self.countTextField?.text ?? "1",self.cartModel);
        }
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated);
    }
    
    //UITextFieldDelegate
    func textFieldDidEndEditing(_ textField: UITextField) {
        let text = textField.text;
        let nsstringStr = text as NSString?
        
        if let num = nsstringStr?.integerValue , num < 1{
            textField.text = "1";
        }
        
    }
    
    @objc func setModel(_ model:YQPCartModel){
        self.cartModel = model
        self.titleLb.text = model.skuName;
        self.selectBtn.isSelected = cartModel.selectState;
        let price = Double(model.price)
        self.countTextField.text = model.itemNumber;
        self.priceLb.text = kYQLSSwift("$" + String(format: "%.2f", price ?? 0.00));
        self.specificationLb.text = model.specification;
        if model.skuImg.count > 0 {
            self.goodsIcon.setImageWith(URL(string: model.skuImg), placeholder: nil)
        }
        

        if model.selectState {
            self.selectBtn.isSelected = true
            
        }else{
            self.selectBtn.isSelected = false
            
        }
    }

}

